package com.atguigu.adddemo1;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Num {

    private int number = 0;

    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    public void add() throws InterruptedException {
        lock.lock();
        try {
            while (number != 0){
               condition.await();
            }
            number++;
            System.out.println(Thread.currentThread().getName() + "线程，number的值是：" + number);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
    public void sub() throws InterruptedException {
        lock.lock();
        try {
            while (number != 1){
               condition.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName() + "线程，number的值是：" + number);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
}
